import { createStore } from 'vuex';
import { register as apiRegister, login as apiLogin, getMe } from '@/api/user';

const loadState = () => {
  try {
    const serializedState = localStorage.getItem('carbonState');
    return serializedState ? JSON.parse(serializedState) : undefined;
  } catch (e) {
    return undefined;
  }
};

const saveState = (state) => {
  localStorage.setItem('carbonState', JSON.stringify(state));
};

const initialState = {
  user: null,
  users: [],
  calculations: [],
  currentEmission: null
};

const store = createStore({
  state: loadState() || initialState,
  mutations: {
    SET_USER(state, user) {
      state.user = user;
      if (user && !state.users.some(u => u.email === user.email)) {
        state.users.push(user);
      }
    },
    ADD_CALCULATION(state, calculation) {
      state.calculations.push({
        ...calculation,
        timestamp: new Date().toISOString()
      });
    },
    LOGOUT(state) {
      state.user = null;
      localStorage.removeItem('token');
    },
    CLEAR_CALCULATIONS(state) {
      state.calculations = [];
    }
  },
  actions: {
    async register({ commit }, user) {
      try {
        const res = await apiRegister(user);
        if (res.data.success) {
          localStorage.setItem('token', res.data.token);
          // 获取用户信息
          const me = await getMe();
          commit('SET_USER', me.data.user);
          saveState(this.state);
          return me.data.user;
        } else {
          throw new Error(res.data.message);
        }
      } catch (err) {
        throw new Error(err.response?.data?.message || err.message || '注册失败');
      }
    },
    async login({ commit }, credentials) {
      try {
        const res = await apiLogin(credentials);
        if (res.data.success) {
          localStorage.setItem('token', res.data.token);
          // 获取用户信息
          const me = await getMe();
          commit('SET_USER', me.data.user);
          saveState(this.state);
          return me.data.user;
        } else {
          throw new Error(res.data.message);
        }
      } catch (err) {
        throw new Error(err.response?.data?.message || err.message || '登录失败');
      }
    },
    logout({ commit }) {
      commit('LOGOUT');
      saveState(this.state);
    },
    addCalculation({ commit }, calculation) {
      commit('ADD_CALCULATION', calculation);
      saveState(this.state);
    }
  },
  getters: {
    isAuthenticated: state => !!state.user,
    weeklyData: state => {
      const last7Days = state.calculations
        .slice(-7)
        .map(c => c.total);
      // 如果不足7天，用0填充
      while (last7Days.length < 7) {
        last7Days.unshift(0);
      }
      return last7Days;
    },
    totalCalculations: state => state.calculations.length,
    averageEmission: state => {
      if (state.calculations.length === 0) return 0;
      const total = state.calculations.reduce((sum, calc) => sum + calc.total, 0);
      return total / state.calculations.length;
    }
  }
});

store.subscribe((mutation, state) => {
  saveState(state);
});

export default store;